﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ShamsiCalendar
{
    public  class clsDateConverter
    {
        public struct ShamsiDate
        {
            public int Year;
            public int Mount;
            public int Day;

            public ShamsiDate(int Y, int M, int D)
            {
                Year = Y;
                Mount = M;
                Day = D;
            }
        }

        public static ShamsiDate ToShamsi(DateTime input)
        {
            int[,] ShamsiSumOfDays = {{0, 31, 62, 93, 124, 155, 186, 216, 246, 276, 306, 336, 365}, {0, 31, 62, 93, 124, 155, 186, 216, 246, 276, 306, 336, 366}};
            ShamsiDate SD = new ShamsiDate(input.Year  - 621, 0, 0);
            int toEid=1;
            int shamsiDayOfYear=1;

            if (ShamsiIsLeap(SD.Year - 1) && DateTime.IsLeapYear(SD.Year))
            {
                toEid=80;
            }
            else{
                toEid=79;
            }

            if (input.DayOfYear <= toEid)
            {
                SD.Year -= 1;
                shamsiDayOfYear = 286 + input.DayOfYear;
                if (ShamsiIsLeap(SD.Year) && !DateTime.IsLeapYear(SD.Year))
                    shamsiDayOfYear += 1;
            }
            else
            {
                shamsiDayOfYear = input.DayOfYear - toEid;
            }

           for (int i = 1; i <= 12; i++)
			{
                if (ShamsiIsLeap(SD.Year))
                {
                    if (ShamsiSumOfDays[1, i] >= shamsiDayOfYear)
                    {
                        SD.Mount  = i;
                        SD.Day = shamsiDayOfYear - ShamsiSumOfDays[1, i - 1];
                        break;
                    }
                }
                else
                {
                    if (ShamsiSumOfDays[0, i] >= shamsiDayOfYear)
                    {
                        SD.Mount = i;
                        SD.Day = shamsiDayOfYear - ShamsiSumOfDays[0, i - 1];
                        break;
                    }
                }
			}

           return SD;

        }

        private static Boolean ShamsiIsLeap(int Year)
        {
            int v = Year % 33;
            if (v == 1 || v == 5 || v == 9 || v == 13 || v == 17 || v == 22 || v == 26 || v == 30)
                return true;

            return false;
        }

        public static string ConvertMountToStr(int Mount)
        {
            switch (Mount)
            {
                case 1: return "فروردین";
                case 2: return "اردیبهشت";
                case 3: return "خرداد";
                case 4: return "تیر";
                case 5: return "مرداد";
                case 6: return "شهریور";
                case 7: return "مهر";
                case 8: return "آبان";
                case 9: return "آذر";
                case 10: return "دی";
                case 11: return "بهمن";
                case 12: return "اسفند";
                default: return "-----";
            }
        }

        public static string ConvertDayToStr(DateTime input)
        {
            switch (input.DayOfWeek)
            {
                case DayOfWeek.Friday:
                    return "جمعه";
                case DayOfWeek.Monday:
                    return "دو شنبه";
                case DayOfWeek.Saturday:
                    return "شنبه";
                case DayOfWeek.Sunday:
                    return "یک شنبه";
                case DayOfWeek.Thursday:
                    return "پنج شنبه";
                case DayOfWeek.Tuesday:
                    return "سه شنبه";
                case DayOfWeek.Wednesday:
                    return "چهارشنبه";
                default:
                    return "-----";
            }
        }
    }
}
